package com.tensquare.qa.service;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;

import com.tensquare.qa.dao.ProblemDao;
import com.tensquare.qa.pojo.Problem;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import util.IdWorker;
import util.JwtUtil;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

@Service
public class ProblemService {
    @Autowired
    private IdWorker idWorker;
   @Autowired
   private ProblemDao problemDao;

    @Autowired
    private HttpServletRequest request;

    public void save(Problem problem) {
        Claims claims_user = (Claims) request.getAttribute("claims_user");
        if(claims_user==null){
            throw  new RuntimeException("权限不足");
        }
        problem.setId(idWorker.nextId()+"");

        problemDao.insert(problem);
    }

    public List<Problem> findAll() {
        return problemDao.selectList(null);

    }

    public Problem findById(String problemId) {
        return problemDao.selectById(problemId);
    }

    public void updateByProblemId(Problem problem) {
        problemDao.updateById(problem);
    }

    public void deleteByProblemId(String problemId) {
        problemDao.deleteById(problemId);
    }

    public Page<Problem> findByPage(Map<String, Object> map, Integer page, Integer size) {
        EntityWrapper<Problem> wrapper=new EntityWrapper<>();
        Set<String> set=map.keySet();
        for (String s : set) {
            wrapper.eq(map.get(s)!=null,s,map.get(s));
        }
        Page<Problem> page1=new Page<>(page,size);
        List<Problem> problems = problemDao.selectPage(page1, wrapper);
        page1.setRecords(problems);
        return page1;
    }

    public List<Problem> findBySearch(Map<String, Object> map) {
        return problemDao.selectByMap(map);
    }

    public Page newList(String label, Integer page, Integer size) {
        Page page1=new Page(page,size);
        List<Problem>list=new ArrayList<>();
        if(label.equals("0")){
           list=problemDao.newList2(page1);
            page1.setRecords(list);
            return page1;
        }
        list=problemDao.newList(label,page1);
        page1.setRecords(list);
        return page1;
    }

    public Page hotlist(String label, Integer page, Integer size) {
        Page page1=new Page(page,size);
        List<Problem>list=problemDao.hotlist(label,page1);
        page1.setRecords(list);
        return page1;
    }

    public Page waitlist(String label, Integer page, Integer size) {
        Page page1=new Page(page,size);
        List<Problem>list=problemDao.waitlist(label,page1);
        page1.setRecords(list);
        return page1;
    }
}
